home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KoPoint.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  4.4 KB  |  129 lines

  1. /* This file is part of the KDE project
  2.    Copyright (C) 2001 David Faure <faure@kde.org>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.  * Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. #ifndef koPoint_h
  21. #define koPoint_h
  22.  
  23. #include <qwmatrix.h>
  24. #include <math.h>
  25.  
  26. /**
  27.  * A point whose coordinates are floating-point values ( "double"s ).
  28.  * The API isn't documented, it's a perfect mirror of QPoint.
  29.  */
  30. class KoPoint {
  31.  
  32. public:
  33.     KoPoint() { m_x = 0; m_y = 0; }
  34.     KoPoint(const double &x, const double &y) : m_x(x), m_y(y) {}
  35.     explicit KoPoint(const QPoint & p) : m_x(p.x()), m_y(p.y()) {}
  36.     ~KoPoint() {}
  37.  
  38.     bool operator==(const KoPoint &rhs) const { return QABS(m_x-rhs.x()) < 1E-10 && QABS(m_y-rhs.y()) < 1E-10; }
  39.     bool operator!=(const KoPoint &rhs) const { return QABS(m_x-rhs.x()) > 1E-10 || QABS(m_y-rhs.y()) > 1E-10; }
  40.  
  41.     bool isNull() const { return m_x == 0 && m_y == 0; }
  42.  
  43.     double x() const { return m_x; }
  44.     double y() const { return m_y; }
  45.     void setX(const double &x) { m_x = x; }
  46.     void setY(const double &y) { m_y = y; }
  47.  
  48.     double &rx() { return m_x; }
  49.     double &ry() { return m_y; }
  50.  
  51.     KoPoint &operator=(const KoPoint &rhs) { m_x = rhs.x(); m_y = rhs.y(); return *this; }
  52.     KoPoint &operator+=( const KoPoint &rhs ) { m_x += rhs.x(); m_y += rhs.y(); return *this; }
  53.     KoPoint &operator-=( const KoPoint &rhs ) { m_x -= rhs.x(); m_y -= rhs.y(); return *this; }
  54.     KoPoint &operator*=( const double &c ) { m_x *= c; m_y *= c; return *this; }
  55.  
  56.     friend inline KoPoint operator+( const KoPoint &, const KoPoint & );
  57.     friend inline KoPoint operator-( const KoPoint &, const KoPoint & );
  58.     friend inline KoPoint operator*( const KoPoint &, const double & );
  59.     friend inline KoPoint operator*( const double &, const KoPoint & );
  60.     friend inline double  operator*( const KoPoint &a, const KoPoint &b );
  61.  
  62.     // Not in QPoint:
  63.     void setCoords(const double &x, const double &y) { m_x = x; m_y = y; }
  64.     KoPoint transform (const QWMatrix &m) const
  65.     {
  66.       double x, y;
  67.       m.map(m_x, m_y, &x, &y);
  68.       return KoPoint(x, y);
  69.     };
  70.  
  71.     bool isNear(const KoPoint &p, double range) const
  72.     {
  73.       return (p.x() >= m_x - range && p.x() <= m_x + range && p.y() >= m_y - range && p.y() <= m_y + range);
  74.     }
  75.  
  76.     static double getAngle( const KoPoint& p1, const KoPoint& p2 ) {
  77.     double a = atan2( p2.x() - p1.x(), p2.y() - p1.y() ) + M_PI;
  78.     return ( ( - ( a * 360 ) / ( 2 * M_PI ) - 90 ) - 180 );
  79.     }
  80.  
  81.     double manhattanLength() const
  82.     {
  83.       return QABS( m_x ) + QABS( m_y );
  84.     }
  85.  
  86.     /// Convert to a QPoint - with precision loss!
  87.     QPoint toQPoint() const
  88.     {
  89.       return QPoint( qRound( m_x ), qRound( m_y ) );
  90.     }
  91.  
  92. private:
  93.     double m_x, m_y;
  94. };
  95.  
  96. inline KoPoint operator+( const KoPoint &p1, const KoPoint &p2 )
  97. { return KoPoint( p1.m_x+p2.m_x, p1.m_y+p2.m_y ); }
  98.  
  99. inline KoPoint operator-( const KoPoint &p1, const KoPoint &p2 )
  100. { return KoPoint( p1.m_x-p2.m_x, p1.m_y-p2.m_y ); }
  101.  
  102. inline KoPoint operator*( const KoPoint &p, const double &c )
  103. { return KoPoint( p.m_x*c, p.m_y*c ); }
  104.  
  105. inline KoPoint operator*( const double &c, const KoPoint &p )
  106. { return KoPoint( p.m_x*c, p.m_y*c ); }
  107.  
  108. inline double operator*( const KoPoint &a, const KoPoint &b )
  109. { return a.m_x * b.m_x + a.m_y * b.m_y; }
  110.  
  111. /******************************
  112.   kdDebug support
  113. *******************************/
  114.  
  115. #include <kdebug.h>
  116.  
  117. /** Show a floating point value with great precision (use within kdDebug) */
  118. #define DEBUGDOUBLE(d) QString::number( (d), 'g', 20 )
  119.  
  120. inline kdbgstream operator<<( kdbgstream str, const KoPoint & r )  {
  121.     // should this use DEBUGDOUBLE?
  122.     str << "(" << r.x() << ", " << r.y() << ")";
  123.     return str;
  124. }
  125.  
  126. inline kndbgstream operator<<( kndbgstream str, const KoPoint & )  { return str; }
  127.  
  128. #endif
  129.